package com.liming.web.UI;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.liming.domain.Book;
import com.liming.domain.BuyCar;
import com.liming.domain.BuyCarItem;
import com.liming.domain.User;
import com.liming.utils.DBUtils;

public class BuyCarfoUI extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getSession(false).removeAttribute("buycar");
		BuyCar buycar=(BuyCar) request.getSession().getAttribute("buycar");
		if(buycar==null){
			buycar=new BuyCar();
			request.getSession().setAttribute("buycar", buycar);
		}
		
		User user=(User)request.getSession().getAttribute("user");
		String sql="select Books.B_id,B_name,B_class,B_author, B_publish,B_brief,B_src,B_fix_price,pick_num from Pickbooks,Books where U_username=\'"+user.getUsername()+"\' and Pickbooks.B_id=Books.B_id";
		System.out.println(sql);
		Connection conn=DBUtils.open();
		try {
			Statement s=conn.createStatement();
			ResultSet re= s.executeQuery(sql);
			while(re.next()){
			
			BuyCarItem buycaritem=new BuyCarItem();
			Book book=new Book();
			book.setbId(re.getString("B_id"));
			book.setbName(re.getString("B_name"));
			book.setbClass(re.getString("B_class"));
			book.setbAuthor(re.getString("B_author"));
			book.setbPublish(re.getString("B_publish"));
			book.setbBrief(re.getString("B_brief"));
			book.setbSrc(re.getString("B_src"));
			book.setbFixprice(re.getDouble("B_fix_price"));
			buycaritem.setBook(book);
			buycaritem.setNum(re.getInt("pick_num"));
			buycar.getItemmap().put(book.getbId(), buycaritem);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		request.getSession().setAttribute("buycar", buycar);
		request.getRequestDispatcher("/WEB-INF/jsp/mybuycarlist.jsp").forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);

	}

}
